import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/config/kq_core_global.dart';
import 'package:kq_flutter_widgets/resources/l10n/kq_intl.dart';
import 'package:kq_flutter_widgets/special/progressForScienceTestMoldEvaluate/progress_for_science_test_mold_evaluate.dart';
import 'package:kq_flutter_widgets/widgets/divider/kq_divider.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';

/// @author 高裕
/// 科学试模评价进度条视图
class SimpleScienceTestMoldEvaluateView extends StatefulWidget {
  const SimpleScienceTestMoldEvaluateView({
    super.key,
    required this.gradientColor,
    required this.cutPart,
    required this.progress,
    required this.title,
    this.tipImage,
    this.onTipTap,
    this.onEmptyTap,
  });

  /// 标题
  final String title;

  /// 提示
  final Function()? onTipTap;

  /// 提示图标
  final String? tipImage;

  /// 点击无数据时的布局
  final Function()? onEmptyTap;

  /// 渐变色
  final List<Color> gradientColor;

  /// 切成几段
  final int cutPart;

  /// 进度 0 ~ 100
  final int? progress;

  @override
  SimpleScienceTestMoldEvaluateViewState createState() =>
      SimpleScienceTestMoldEvaluateViewState();
}

class SimpleScienceTestMoldEvaluateViewState
    extends State<SimpleScienceTestMoldEvaluateView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: KqThemeColors.bgWhite,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 14.r),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  children: [
                    Text(
                      widget.title,
                      style: TextStyle(
                        color: KqThemeColors.text26,
                        fontSize: 14.sp,
                      ),
                    ),
                    if (widget.tipImage != null && widget.tipImage != '')
                      Container(
                        margin: EdgeInsets.only(left: 2.r),
                        child: KqInkWell(
                          onTap: widget.onTipTap,
                          child: KqImage.assets(
                            url: widget.tipImage ?? '',
                            width: 16.r,
                            height: 16.r,
                          ),
                        ),
                      ),
                  ],
                ),
                SizedBox(
                  height: 2.r,
                ),
                if (widget.progress != null)
                  ProgressForScienceTestMoldEvaluate(
                    gradientColor: widget.gradientColor,
                    cutPart: widget.cutPart,
                    progress: widget.progress ?? 0,
                    topCustomView: (dx, maxWidth) {
                      double width = paintWidthWithTextStyle(
                          widget.progress.toString(), 14.sp);
                      double textDx = dx - (width / 2) + 2.r;
                      return SizedBox(
                        height: 18.r,
                        child: OverflowBox(
                          alignment: Alignment.topLeft,
                          maxWidth: MediaQuery.of(KqCoreGlobal.globalContext)
                              .size
                              .width,
                          maxHeight: 18.r,
                          child: Container(
                            width: width,
                            margin: textDx >= 0
                                ? EdgeInsets.only(left: textDx)
                                : EdgeInsets.only(
                                    right: maxWidth - textDx - width),
                            // 数字有误差
                            child: Text(
                              widget.progress.toString(),
                              style: TextStyle(fontSize: 14.sp),
                            ),
                          ),
                        ),
                      );
                    },
                    bottomCustomView: (dx, maxWidth) {
                      return SizedBox(
                        height: 20.r,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            Text(
                              KqIntl.currentResource.low,
                              style: TextStyle(fontSize: 12.sp),
                            ),
                            const Expanded(child: SizedBox()),
                            Text(
                              KqIntl.currentResource.middle,
                              style: TextStyle(fontSize: 12.sp),
                            ),
                            const Expanded(child: SizedBox()),
                            Text(
                              KqIntl.currentResource.high,
                              style: TextStyle(fontSize: 12.sp),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                if (widget.progress == null)
                  KqInkWell(
                    onTap: widget.onEmptyTap,
                    enableRipple: false,
                    child: SizedBox(
                      height: 38.r,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            KqIntl.currentResource.canNotCount,
                            style: TextStyle(
                                color: KqThemeColors.text26,
                                fontSize: 16.sp,
                                fontWeight: FontWeight.w500),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 2.r),
                            child: KqImage.assets(
                              url: Images.commonIcBangzhu,
                              package: KqGlobal.packageName,
                              width: 16.r,
                              height: 16.r,
                            ),
                          ),
                        ],
                      ),
                    ),
                  )
              ],
            ),
          ),
          const KqDivider(),
        ],
      ),
    );
  }

  /// 计算文本宽度
  double paintWidthWithTextStyle(String text, double fontSize) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: TextStyle(fontSize: fontSize)),
        maxLines: 1,
        textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size.width;
  }
}
